package com.huazi.ucenter.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.huazi.commonutils.CommentResult;
import com.huazi.commonutils.JwtUtils;
import com.huazi.commonutils.MemberOrder;
import com.huazi.commonutils.PhoneUtils;
import com.huazi.servicebase.exceptionhandler.MyException;
import com.huazi.ucenter.entity.Member;
import com.huazi.ucenter.entity.Register;
import com.huazi.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-27
 */
@RestController
@CrossOrigin
@RequestMapping("/educenter/member")
public class MemberController {
    @Autowired
    private  MemberService memberService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //会员登录
    @PostMapping("login")
    public CommentResult login(@RequestBody Member member){
        //调用service实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return CommentResult.getOK().data("token",token);
    }
    //会员注册
    @PostMapping("register")
    public CommentResult register(@RequestBody Register register){
        CommentResult save = memberService.register(register);
        return save;

    }
    //会员退出登录
    @PostMapping("logout")
    public CommentResult logout(@RequestBody Member member){

        //调用service实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return CommentResult.getOK().data("token",token);
    }
    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public CommentResult getMemberInfo(HttpServletRequest request) {
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            //根据id获取用户信息并返回
//            System.out.println("---"+memberId);
            Member member = memberService.getById(memberId);
//            System.out.println(member);
            return CommentResult.getOK().data("member", member);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException("error");
        }
    }
    //根据用户id获取用户信息(生成订单信息)
    @GetMapping("getMemberOrderInfo/{id}")
    public MemberOrder getMemberOrderInfo(@PathVariable("id") String id){
//        System.out.println(id);
        Member member = memberService.getById(id);
//        System.out.println("mem:"+member);
        MemberOrder memberOrder=new MemberOrder();
        //使用工具复制对象
        BeanUtils.copyProperties(member,memberOrder);
        return memberOrder;
    }
    //查询某天的注册人数
    @GetMapping("countRegister/{day}")
    public CommentResult countRegister(@PathVariable String day){
        Integer integer = memberService.countRegister(day);
        return CommentResult.getOK().data("count",integer);
    }
}

